﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;


namespace MiniBug.Controllers
{
    public abstract class HandleErrorController : Controller
    {
        protected override void HandleUnknownAction(string actionName)
        {
            //if controller is ErrorController dont 'nest' exceptions
            if (this.GetType() != typeof(ErrorController))
                this.InvokeHttp404(HttpContext);
        }

        public ActionResult InvokeHttp404(HttpContextBase httpContext)
        {
            //IController errorController = ObjectFactory.GetInstance<ErrorController>();
            //var errorRoute = new RouteData();
            //errorRoute.Values.Add("controller", "Error");
            //errorRoute.Values.Add("action", "Http404");
            //errorRoute.Values.Add("url", httpContext.Request.Url.OriginalString);
            //errorController.Execute(new RequestContext(httpContext, errorRoute));

            return new EmptyResult();
        }

    }

    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        //protected override IController GetControllerInstance(Type controllerType)
        //{
        //    try
        //    {
        //        if (controllerType == null)
        //            return base.GetControllerInstance(RequestContext.HttpContext,controllerType);
        //    }
        //    catch (HttpException ex)
        //    {
        //        if (ex.GetHttpCode() == (int)HttpStatusCode.NotFound)
        //        {
        //            IController errorController = ObjectFactory.GetInstance<ErrorController>();
        //            ((ErrorController)errorController).InvokeHttp404(RequestContext.HttpContext);

        //            return errorController;
        //        }
        //        else
        //            throw ex;
        //    }

        //    return ObjectFactory.GetInstance(controllerType) as Controller;
        //}
    }

}
